package cn.passlink.bletoothchat.SqlTables;

import android.content.Context;
import android.database.DatabaseErrorHandler;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Build;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class MyDatabaseHelper extends SQLiteOpenHelper {
    public MyDatabaseHelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    public MyDatabaseHelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version, @Nullable DatabaseErrorHandler errorHandler) {
        super(context, name, factory, version, errorHandler);
    }

    @RequiresApi(api = Build.VERSION_CODES.P)
    public MyDatabaseHelper(@Nullable Context context, @Nullable String name, int version, @NonNull SQLiteDatabase.OpenParams openParams) {
        super(context, name, version, openParams);
    }


    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
    private Context mContext;

    public MyDatabaseHelper(Context context , int version) {
        super(context, "bluetoothchat.db", null, version);
        mContext = context;

    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        try {
            // 读取并执行 mbloothdatabase.sql 文件中的 SQL 语句
            InputStream inputStream = mContext.getAssets().open("mbloothdatabase.sql");
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                db.execSQL(line);
            }
            Toast.makeText(mContext, "bluetoothchat数据库创建成功！", Toast.LENGTH_SHORT).show();
            bufferedReader.close();
        } catch (IOException e) {
            Toast.makeText(mContext, "数据库创建失败！", Toast.LENGTH_SHORT).show();
            e.printStackTrace();
        }
    }


}
